package com.tmsps.common.util.time;

import com.tmsps.ne4spring.utils.ChkUtil;
import com.tmsps.ne4spring.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 日期辅助工具类
 * 继承com.tmsps.ne4spring.utils.TimeUtil
 *
 * @author geek
 */
@Slf4j
public class SysTimeUtil extends TimeUtil{

	/**
	 * 获取当前时间long值
	 * 默认模式 yyyyMMddHHmmssSSS
	 *
	 * @return
	 */
	public static Long getCurrentTime() {
		return Long.valueOf(TimeUtil.getCurrentTime("yyyyMMddHHmmssSSS"));
	}

	/**
	 * 获取当前时间long值
	 *
	 * @param pattern 模式
	 * @return long
	 */
	public static Long getCurrentTimeLong(String pattern) {
		return Long.valueOf(TimeUtil.getCurrentTime(pattern));
	}

	/**
	 * 获取给定时间long值
	 *
	 * @param date 日期
	 * @return long
	 */
	public static Long getTimeLong(String date ) {
		if (ChkUtil.isNull(date)) {
			return null;
		}
		String pattern = "yyyy-MM-dd";
		Date date1 = strToDate(date, pattern);
		return Long.valueOf(getTiemModle(date1,pattern));
	}

	/**
	 * 获取给定时间long值
	 *
	 * @param date 日期
	 * @param pattern 模式
	 * @return long
	 */
	public static Long getTimeLong(String date, String pattern) {
		if (ChkUtil.isNull(date)) {
			return null;
		}
		Date date1 = strToDate(date, pattern);
		return Long.valueOf(getTiemModle(date1,pattern));
	}

	/**
	 * 获取今天long值
	 * 默认模式 yyyyMMdd
	 *
	 * @return long
	 */
	public static Long getToday() {
		return Long.valueOf(TimeUtil.getCurrentTime("yyyyMMdd000000000"));
	}

	/**
	 * 获取明天long值
	 * 默认模式 yyyyMMdd
	 *
	 * @return long
	 */
	public static Long getNextDay() {
		return Long.valueOf(TimeUtil.getNextTime("yyyyMMdd000000000"));
	}

	/**
	 * 获取当日剩余时间 秒 long值
	 *
	 * @return long
	 */
	public static Long getTimeLeftSeconds() {
		LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
		long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
		return seconds;
	}

	/**
	 * 获取当日剩余时间 毫秒 long值
	 *
	 * @return long
	 */
	public static Long getTimeLeftMillSeconds() {
		LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(),midnight);
		return millSeconds;
	}

	public static Date strToDate(@NotNull String date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date d = sdf.parse(date);
			return new java.sql.Date(d.getTime());
		} catch (ParseException e) {
			log.error("ParseException",e);
		}
		return null;
	}


}
